$(document).ready(function() {
    var $locationCountry = $('#location_country');
    var $locationRegion = $('#location_region');
    var $locationCity = $('#location_city');

    $locationCountry.change(function() {
        $locationRegion.find('option[value!=""]').remove();
        $locationCity.find('option[value!=""]').remove();

        $.ajax({
            url: Routing.generate('location_get_regions_for_country', {id: $locationCountry.val()}),
            method: 'GET',
            success: function (regions) {
                regions.forEach(function(region) {
                    $locationRegion.append('<option value="' + region.id + '">' + region.name + '</option>');
                });
            }
        });
    });

    $locationRegion.change(function() {
        $locationCity.find('option[value!=""]').remove();

        $.ajax({
            url: Routing.generate('location_get_cities_for_region', {id: $locationRegion.val()}),
            method: 'GET',
            success: function (cities) {
                cities.forEach(function(city) {
                    $locationCity.append('<option value="' + city.id + '">' + city.name + '</option>');
                });
            }
        });
    });
});
